package wowraidmanager.server.service.wishlist.news;

import java.util.ArrayList;

import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import wowraidmanager.client.service.wishlist.news.NewslistService;
import wowraidmanager.server.meta.wishlist.NewslistMeta;
import wowraidmanager.shared.model.wishlist.Newslist;

public class NewslistServiceImpl implements NewslistService{

    NewslistMeta query = NewslistMeta.get();

    public void setNewslist(Newslist newslist){
        Datastore.put(newslist);
    }

    public ArrayList<Newslist> getNewslist(){
        return (ArrayList<Newslist>)Datastore.query(query).sort(query.createDate.desc).asList();
    }

    public void deleteNewsbyKey(String targetNews){
        Key targetKey = getKeyById(targetNews);
        Datastore.delete(targetKey);
    }

    public Key getKeyById(String targetUser){
        String[] targetUserSplit = targetUser.split("\\(");
        String kind = targetUserSplit[0];
        long id = Long.valueOf(targetUserSplit[1].substring(0, targetUserSplit[1].length() - 1));

        return KeyFactory.createKey(kind, id);
    }
}
